package com.tfjybj.itoo.exam.provider.controller;

import org.apache.dubbo.config.annotation.Reference;;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.ProfessionEntity;
import com.tfjybj.itoo.exam.provider.service.ProfessionService;
import com.tfjybj.itoo.teach.FacadeApi.ProfessionApi;
import com.tfjybj.itoo.teach.model.ProfessionModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * ProfessionController
 * profession表
 *
 * @author 杨江涛
 * @version 2.0.0
 * @since 2.0.0 2018-11-15 10:35:56
 */
@Api(tags = {"profession表接口"})
@RequestMapping(value = "/profession")
@RestController
public class ProfessionController {

    @Resource
    private ProfessionService professionService;

    @Reference(check = false)
    private ProfessionApi professionApi;


    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model ProfessionModel
     * @return 添加的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody ProfessionModel model) {
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        return professionApi.create(model);
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        if (StringUtils.isEmpty(id)) {
            return ItooResult.build(ItooResult.FAIL, "id为空");
        }
        return professionApi.delete(id);
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return ItooResult.build(ItooResult.FAIL, "ids为空");
        }
        return professionApi.deleteByIds(ids);
    }

    /**
     * 修改
     *
     * @param model ProfessionModel
     * @return 修改后的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id修改profession")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody ProfessionModel model) {
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        return professionApi.modify(model);
    }

    /**
     * 根据id查找Profession
     *
     * @param id 主键id
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        if (StringUtils.isEmpty(id)) {
            return ItooResult.build(ItooResult.FAIL, "id为空");
        }
        return professionApi.findById(id);
    }

    /**
     * 分页查询所有Profession
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "分页查询所有Profession")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        return professionApi.queryPageAll(pageNo, pageSize);
    }

    /**
     * 根据专业名称模糊查询profession
     *
     * @param name     专业名称
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的profession
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据专业名称模糊查询profession", notes = "分页根据专业名称模糊查询profession")
    @GetMapping(value = "queryProfessionByLikeName/{pageNo}/{pageSize}")
    public ItooResult queryProfessionByLikeName(@RequestParam(required = false, defaultValue = "") String name,
                                                @ApiParam(value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                                @ApiParam(value = "记录数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<ProfessionEntity> professionList = professionService.queryByLikeName(name, pageNo, pageSize);
        return professionApi.queryProfessionByLikeName(name, pageNo, pageSize);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 根据academyId查找专业id，专业代码，专业名称，专业性质，
     *
     * @param academyId 主键academyId
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据学院academyId查询专业信息")
    @GetMapping(value = {"/findByAcademyId/{academyId}"})
    public ItooResult findByAcademyId(@ApiParam(value = "academyId", required = true) @PathVariable String academyId) {
        if (StringUtils.isEmpty(academyId)) {
            return ItooResult.build(ItooResult.FAIL, "查询失败，学院ID为空");
        }
        return professionService.findByAcademyId(academyId);
    }


    /**
     * 根据学院id,查询符合条件的专业
     *
     * @param academyId 学院id
     * @return 专业集合
     * @author 马莹
     * @since 2019-4-30 21:19:55
     */
    @ApiOperation(value = "根据学院id查询专业信息,附加上班级个数")
    @GetMapping(value = "/inquirySpecialty/{academyId}")
    public ItooResult inquirySpecialty(@PathVariable String academyId) {
        if (StringUtils.isEmpty(academyId)) {
            return ItooResult.build(ItooResult.FAIL, "查询失败，学院ID为空");
        }
        return ItooResult.build(ItooResult.SUCCESS, "查询成功!", professionService.inquirySpecialty(academyId));
    }
}
